package com.yunhe.authority.controller.authority;

import com.yunhe.authority.domain.authority.BackUser;
import com.yunhe.authority.domain.authority.User;
import com.yunhe.authority.service.authority.BackUserService;
import com.yunhe.authority.util.MyHeaderUtil;
import com.yunhe.common.annotation.OperationRecord;
import com.yunhe.common.constant.SystemConsts;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.model.PageParam;
import com.yunhe.common.model.ResultObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhengbo
 * @date 2021/10/20 14:06
 */
@Api(value = "BackUser", description = "后台用户维护", tags = {"后台用户维护"})
@RestController
@RequestMapping("/backUsers")
public class BackUserController {

    @Autowired
    private MyHeaderUtil myHeaderUtil;

    @Autowired
    private BackUserService backUserService;

    /**
     * 新增后台用户
     * @param backUser 用户对象
     */
    @ApiOperation("新增后台用户")
    @RequestMapping(value = "/creatBackstageUser", method = RequestMethod.POST)
    @OperationRecord(menu = "后台用户管理", actionType = SystemConsts.ACTION.ADD)
    public ResponseEntity<ResultObject<BackUser>> creatBackstageUser(@RequestParam(value = "roleIds") String roleIds,
                                                                     @RequestBody BackUser backUser)
            throws ArgumentErrorException{
        BackUser headerBackUser = myHeaderUtil.getHeaderBackUser();
        if (headerBackUser != null) {
            backUser.setCreator(headerBackUser.getTitle());
        }
        BackUser newUser = backUserService.save(backUser,roleIds);
        return new ResponseEntity<>(new ResultObject<>(newUser), HttpStatus.OK);
    }

    /**
     * 移除用户
     * @param id 用户id
     */
    @ApiOperation("移除后台用户")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @OperationRecord(menu = "移除后台用户", actionType = SystemConsts.ACTION.DELETE)
    public ResponseEntity<ResultObject<String>> creatBackstageUser(@RequestParam(value = "id") Long id) {
        backUserService.deleteById(id);
        return new ResponseEntity<>(new ResultObject<>("success"), HttpStatus.OK);
    }

    /**
     * 根据角色ID获得后台用户列表
     * @param id 角色ID
     * @param keywords 关键词
     */
    @ApiOperation("根据角色ID获得后台用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "keywords", value = "关键词", required = false, dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/findUsersById", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> findUsersById(@RequestParam(value = "id") Long id,
                                                      @RequestParam(value = "keywords", required = false) String keywords,
                                                      @ModelAttribute PageParam pageParam) {
        Page<BackUser> users = backUserService.findUsersByIdAndCondition(id, keywords, pageParam);
        return new ResponseEntity<>(new ResultObject<>(users), HttpStatus.OK);
    }
}
